<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root">

    </div>
</body>
<script src="../react.js"></script>
<script src="../react-dom.js"></script>
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
<script type="text/babel">
    class App extends React.Component{
        red={
            backgroundColor:"red"
        }
        state={
            list:[
                {
                    name:"辣条",
                    price:5,
                    num:1
                },{
                    name:"巧克力",
                    price:35,
                    num:2
                },{
                    name:"奶茶",
                    price:15,
                    num:1
                },{
                    name:"烤箱",
                    price:2099,
                    num:1
                }
            ]
        }
        render(){
            let {list} = this.state;
            return (
                <div>
                    <table border='1'>
                        <thead>
                            <tr>
                                <th>名称</th>
                                <th>价格</th>
                                <th>数量</th>
                                <th>操作</th>    
                            </tr>    
                        </thead>
                        <tbody>
                            {
                                list.map((item,index)=>{    
                                    return (
                                        <tr key={index}>
                                            <td>{item.name}</td>
                                            <td>{item.price}</td>
                                            <td>
                                                <button onClick={()=>this.reduce(index)} style={item.num<=0?this.red:{}}>-</button>
                                            {item.num}
                                                <button onClick={()=>this.add(index)} style={item.num>=10?this.red:{}}>+</button>
                                            </td>
                                            <td>
                                                <button onClick={()=>this.reomve(index)}>删除</button>
                                            </td>
                                        </tr>
                                    )
                                })
                            }
                        </tbody>
                    </table> 
                    <h1>总价：{this.getSum()}</h1>   
                </div>
            )
        }
        // 总价
        getSum(){
            let {list} = this.state;
            let sum = 0;
            list.forEach((item,index)=>{
                let goods = item.price * item.num
                console.log(goods)
                sum+=goods;
            })
            return sum
        }
        reomve(index){
            let {list} = this.state;
            list.splice(index,1);
            this.setState({
                list:list
            })
        }
        add(index){
            let {list} = this.state;
            if(list[index].num>=10){
                return
            }
            // 根据索引找到当前点击的商品
            list[index].num ++;
            this.setState({
                list:list
            })
        }
        reduce(index){
            let {list} = this.state;
            if(list[index].num<=0){
                return
            }
            // 根据索引找到当前点击的商品
            list[index].num --;
            this.setState({
                list:list
            })
        }
    }


    ReactDOM.render(<App/>,document.getElementById("root"))
</script>
</html>